package org.apache.servicecomb.fence.service;

import java.util.concurrent.CompletableFuture;

import org.apache.seata.core.context.RootContext;
import org.apache.servicecomb.core.Invocation;
import org.apache.servicecomb.core.filter.AbstractFilter;
import org.apache.servicecomb.core.filter.ConsumerFilter;
import org.apache.servicecomb.core.filter.FilterNode;
import org.apache.servicecomb.swagger.invocation.Response;

public class SeataContextConsumerFilter extends AbstractFilter implements ConsumerFilter {
  @Override
  public int getOrder() {
    return 0;
  }

  @Override
  public String getName() {
    return "seata-consumer";
  }

  @Override
  public CompletableFuture<Response> onFilter(Invocation invocation, FilterNode nextNode) {
    String xid = RootContext.getXID();
    if (xid != null) {
      invocation.addContext(RootContext.KEY_XID, xid);
    }
    return nextNode.onFilter(invocation);
  }
}
